#pragma once
#include<iostream>
#include<ctime>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>

using namespace std;
#define TaskNum 3
typedef void(*task_t)();

task_t tasks[TaskNum];

void Print()
{
    cout<<"I am Print Task"<<endl;
}

void Download()
{
    cout<<"I am Download Task"<<endl;
}

void Flush()
{
    cout<<"I am Flush Task"<<endl;
}

void LoadTask()
{
    srand(time(nullptr)^getpid());
    tasks[0]= Print;
    tasks[1]= Download;
    tasks[2]= Flush;
}

void ExcuteTask(int number)
{
    if(number<0||number>TaskNum)return;
    tasks[number]();
}

int SelectTask()
{
    return (rand()%TaskNum);
    //return 0;
}

void work()
{
    while(true)
    {
        sleep(1);
        int command=0;
        int n=read(0,&command,sizeof(command));//从标准输入去读
        if(n==sizeof(int))
        {
            cout<<"pid is :"<<getpid()<<" handler task "<<endl;
            ExcuteTask(command);
        }
        else if(n==0)
        {
            cout<<"sub process: "<<getpid()<<" quit"<<endl;
        }
    }
}